package com.cxs.controller;

import com.cxs.entity.Clazz;
import com.cxs.entity.College;
import com.cxs.service.AdminService;
import com.cxs.service.ClassroomService;
import com.cxs.service.ClazzService;
import com.cxs.service.CollegeService;
import com.cxs.service.SeatOrderService;
import com.cxs.service.SeatService;
import com.cxs.entity.vo.ClazzVO;
import com.cxs.entity.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/*
 * @Project:seat-subscribe-sys
 * @Author:cxs
 * @Motto:放下杂念,只为迎接明天更好的自己
 * */
@Controller
@RequestMapping("/clazz")
public class ClazzController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private ClassroomService classroomService;

    @Autowired
    private SeatService seatService;

    @Autowired
    private SeatOrderService seatOrderService;

    @Autowired
    private CollegeService collegeService;

    @Autowired
    private ClazzService clazzService;


    @RequestMapping("/toClazzList")
    public String toClazzList(HttpSession session, Model model) {
        List<ClazzVO> list = clazzService.selectList();
        model.addAttribute("list", list);
        return "admin/clazzList";
    }

    @RequestMapping("/toAddClazz")
    public String toAddClazz(HttpSession session, Model model) {
        List<College> list = collegeService.list(null);
        model.addAttribute("list", list);
        return "admin/addClazz";
    }

    @RequestMapping("/toEditClazz")
    public String toEditClazz(Integer id, HttpSession session, Model model) {
        Clazz byId = clazzService.getById(id);
        List<College> list = collegeService.list(null);
        model.addAttribute("list", list);
        model.addAttribute("data", byId);
        return "admin/editClazz";
    }

    @RequestMapping("/addClazz")
    @ResponseBody
    public Result addClazz(Clazz Clazz, HttpSession session, Model model) {
        Result result = new Result(200, "OK");
        boolean save = clazzService.save(Clazz);
        if (!save) {
            result.setCode(-1);
            result.setMsg("添加失败");
        }
        return result;
    }

    @RequestMapping("/updateClazz")
    @ResponseBody
    public Result updateClazz(Clazz Clazz, HttpSession session, Model model) {
        Result result = new Result(200, "OK");
        boolean save = clazzService.updateById(Clazz);
        if (!save) {
            result.setCode(-1);
            result.setMsg("修改失败");
        }
        return result;
    }

    @RequestMapping("/deleteClazz")
    @ResponseBody
    public Result deleteClazz(Clazz Clazz, HttpSession session, Model model) {
        Result result = new Result(200, "OK");
        boolean save = clazzService.removeById(Clazz.getId());
        if (!save) {
            result.setCode(-1);
            result.setMsg("删除失败");
        }
        return result;
    }

    @RequestMapping("/getClazzList")
    @ResponseBody
    public Result getClazzList(HttpSession session, Model model) {
        Result result = new Result(200, "OK");
        List<Clazz> list = clazzService.list(null);
        result.setData(list);
        return result;
    }

}
